// pages/control/production_ucharts/production_ucharts.js
const http = require("../../../utils/request");
//TODO:更改阴影
const u_setShadow=[
  1,
  2,
  5,
]

Component({
  data: {
    chartData: {},
    TimeValue:'30',
    //controlCard组件C
    pickerValue:['近30天'],
    pickerOptions:[
      { label: '近30天', value: '30' },
      { label: '近60天', value: '60' },
      { label: '近180天', value: '180' }
    ],
    pickerTitle:"选择图表周期",
    //summary组件数据
    summary:[{  
      title:"日",
      info:"0"
    },{
      title:"月",
      info:"0"
    },{
      title:"年",
      info:"0"
    },],
    comparison:[{  
      title:"同比",
      info:"0%"  //比率
    },{
      title:"环比",
      info:"0%"
    }],
    //t-下拉菜单
    product: {
      value: 'all',
      options: [
        {
          value: 'all',
          label: '全部产品',
        },
        {
          value: 'new',
          label: '最新产品',
        },
        {
          value: 'hot',
          label: '最火产品',
        },
      ],
    },
    //Uchart
    chartDatas: [],
    chartTime:[],
    //您可以通过修改 config-ucharts.js 文件中下标为 ['line'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
    opts: {
        color: ["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
        padding: [15,10,0,15], 
        dataLabel: false,
        dataPointShape: false,
        enableScroll: false,
        legend: {
        },
        xAxis: {
          disableGrid: true,
          labelCount:5,
        },
        yAxis: {
          gridType: "dash",
          dashLength: 2,
          data: [
            {
              min: 0,
            }
          ]
        },
        extra: {
          line: {
            type: "curve",
            width: 10,
            activeType: "hollow",
            linearType: "custom",
            onShadow: true,
            animation: "horizontal"
          }
        }
      }

  },
  /**
   * 组件的方法列表
   */
  methods: {
    //card组件
    receiveTapPicker(e){
      console.log('选择了：'+e.detail.pickerValue[0])
      this.setData({
        TimeValue:e.detail.pickerValue[0]
      })
      http.getRequest("/admin/console/queryCountYoYAndQoQ?count="+this.data.TimeValue,{},res=>{
        this.setData({
          comparison:[{  
            title:"同比",
            info: res.data.data.YoY=="Infinity"?"0%":res.data.data.YoY.toFixed(3)+"%"  //比率
          },{
            title:"环比",
            info: res.data.data.QoQ=="Infinity"?"0%":res.data.data.QoQ.toFixed(3)+"%" 
          }],
        })
       })
       this.getCiShu(this.data.TimeValue).then(data=>{
        let chartDatas = [];
        let chartTime = [];
       for(let v of data){
         chartDatas.push(v.queryCount);
         chartTime.push(v.date.substr(5,))
       }
       console.log(chartTime);
       this.setData({
         chartDatas,
         chartTime
       })
       })
     this.getServerData();
    },
    async getCiShu(time){
      return new Promise((resolve,reject)=>{
        http.getRequest("/admin/console/queryCount?count="+time,{},res=>{
          console.log('次数',time,res.data);
          resolve(res.data.data)
       })
      })
    },
    //uchart
    getServerData() {
      //模拟从服务器获取数据时的延时
      setTimeout(() => {
        //模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
        let res = {
            categories: this.data.chartTime,
            series: [
              {
                name: "扫描次数",
                linearColor: [
                  [
                    0,
                    "#1890FF"
                  ],
                  [
                    0.25,
                    "#00B5FF"
                  ],
                  [
                    0.5,
                    "#00D1ED"
                  ],
                  [
                    0.75,
                    "#00E6BB"
                  ],
                  [
                    1,
                    "#90F489"
                  ]
                ],
                setShadow: [
                  1,
                  2,
                  5,
                  "#1890FF"
                ],
                data: this.data.chartDatas
              }
            ]
          };
        this.setData({ chartData: JSON.parse(JSON.stringify(res)) });
      }, 200);
    },
  },
  lifetimes:{
    ready(){
      http.getRequest("/admin/console/queryCountYoYAndQoQ?count="+this.data.TimeValue,{},res=>{
        this.setData({
          comparison:[{  
            title:"同比",
            info: res.data.data.YoY=="Infinity"?"0%":res.data.data.YoY.toFixed(3)+"%"  //比率
          },{
            title:"环比",
            info: res.data.data.QoQ=="Infinity"?"0%":res.data.data.QoQ.toFixed(3)+"%" 
          }],
        })
       })
       http.getRequest("/admin/console/queryCount?count="+this.data.TimeValue,{},res=>{
          let chartDatas = [];
          let chartTime = [];
          for(let v of res.data.data){
            chartDatas.push(v.queryCount);
            chartTime.push(v.date.substr(5,))
          }
          this.setData({
            chartDatas,
            chartTime
          })
       })
       let dataOne = 0;
       let dataTwo = 0;
       let dataThree = 0;
       this.getCiShu(1).then(v=>{
         console.log(1,v);
         if(v.length==0) dataOne = 0;
         for(let i of v){
           dataOne += i.queryCount;
         }
         this.getCiShu(30).then(v=>{
           console.log(30,v);
           if(v.length==0) dataTwo = 0;
           for(let i of v){
             dataTwo += i.queryCount;
           }
           this.getCiShu(365).then(v=>{
             console.log(1,v);
             if(v.length==0) dataOne = 0;
             for(let i of v){
               dataThree += i.queryCount;
             }
             let summary = [{  
               title:"日",
               info: dataOne
             },{
               title:"月",
               info: dataTwo
             },{
               title:"年",
               info: dataThree
             },]
             this.setData({
               summary
             })
         })
       })
       this.getServerData();
    })
  }
}
})